const _common_util = require('../util/util_1.js');

const _user_service = {
    //用户登录
    login: function (userInfo, resolve, reject) {
        _common_util.request({
            url     : _common_util.getServerURL('/user/login'),
            method  : 'POST',
            data    : userInfo,
            contentType: 'application/x-www-form-urlencoded',
            success : resolve,
            error   : reject
        });
    },
    //用户注册
    register: function(userInfo, resolve, reject) {
        _common_util.request({
            url     : _common_util.getServerURL('/user/register'),
            method  : 'POST',
            data    : userInfo,
            contentType: 'application/x-www-form-urlencoded',
            success : resolve,
            error   : reject
        });
    },
    //检查用户名是否存在
    checkUsername: function(username, resolve, reject) {
        _common_util.request({
            url     : _common_util.getServerURL('/user/check_username?username=' + username),
            method  : 'POST',
            success : resolve,
            error   : reject
        });
    },
    //获取验证码
    getCaptcha: function() {
        // 由于验证码是图片，直接返回图片URL
        return _common_util.getServerURL('/user/captcha');
    },
    //获取所有用户信息
    getAllUsers: function(resolve, reject) {
        _common_util.request({
            url     : _common_util.getServerURL('/users'),
            method  : 'GET',
            success : resolve,
            error   : reject
        });
    }
};

module.exports = _user_service;